package com.wty.swhn.wty.biz.implAsmackImpl;

import android.os.Bundle;
import android.os.Message;
import android.util.Log;

import com.wty.swhn.wty.TApplication;
import com.wty.swhn.wty.biz.IRegisterBiz;
import com.wty.swhn.wty.model.UserEntity;
import com.wty.swhn.wty.util.Const;
import com.wty.swhn.wty.view.RegisterActivity;

import org.jivesoftware.smack.AccountManager;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.SmackConfiguration;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.util.StringUtils;
import org.jivesoftware.smackx.packet.VCard;

import java.util.HashMap;

/**
 * Created by swhn on 2016/8/5.
 * 用户注册
 */
public class RegisterBiz implements IRegisterBiz {
    private final String TAG = "RegisterBiz";

    @Override
    public void register(final RegisterActivity registerActivity, final UserEntity userEntity) {
        new Thread() {
            @Override
            public void run() {
                int status = Const.CONNECT_FAILURE;
                try {
                    // 判断联网是否成功
                    int count = 0;
                    while (count < 6000
                            && TApplication.xmppConnection.isConnected() == false) {
                        this.sleep(10);
                        count++;
                    }
                    if (TApplication.xmppConnection.isConnected()) {
                    	 // 注册功能，联网发数据
                        // 帐户管理
                        AccountManager accountManager = TApplication.xmppConnection
                                .getAccountManager();
                        //上传头像
//                        setUserImage(userEntity.getImage(),userEntity.getUserName());
                     // 放的是用户的属性如name,mobile,mail
                        HashMap<String, String> map = new HashMap<String, String>();
                        map.put("name", userEntity.getName());
                        // 注册
                        accountManager.createAccount(userEntity.getUserName(),
                                userEntity.getPassWord(), map);
                        status = Const.STATUS_SUCCESS;
                    } else {
                        // 联网失败
                        status = Const.CONNECT_FAILURE;
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                    String errorInfo=e.toString();
                    Log.i("registerBiz", errorInfo);
                    //异常有很多种，不同异常errorInfo的值不一样
                    if (errorInfo.equals("conflict(409)"))
                    {
                        //用户已经存在
                        status=Const.STATUS_USER_CONFLICT;
                    }
                } finally {
                    Message message = registerActivity.handler.obtainMessage();
                    message.what = RegisterActivity.MSG_REGISTER;
                    //带数据
                    Bundle bundle = new Bundle();
                    bundle.putInt(Const.KEY_DATA, status);
                    message.setData(bundle);
                    registerActivity.handler.sendMessage(message);
                }
            }

        }.start();
    }
//添加头像
	protected void setUserImage(byte[] image, String userEntiry) {
		
		try {	
			TApplication taApplication = new TApplication();
		XMPPConnection connection = taApplication.getConnection();
		VCard card = new VCard();
			card.load(connection);
			PacketFilter filter = new AndFilter(new PacketIDFilter(
					card.getPacketID()), new PacketTypeFilter(IQ.class));
			PacketCollector collector = connection
					.createPacketCollector(filter);
			String encodeImage = StringUtils.encodeBase64(image);
			card.setAvatar(image, encodeImage);
			card.setEncodedImage(encodeImage);
			card.setField("PHOTO", "<TYPE>image/jpg</TYPE><BINVAL>"
					+ encodeImage + "</BINVAL>", true);
			Log.i("other", "上传头像的方法！");
			card.save(connection);
			IQ iq = (IQ) collector.nextResult(SmackConfiguration
					.getPacketReplyTimeout());
			
		} catch (XMPPException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}


	
    
  
}
